package com.example.mallstable.ui;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.mallstable.R;
import com.example.mallstable.config.Constant;
import com.example.mallstable.pojo.Address;
import com.example.mallstable.pojo.ResponeCode;
import com.example.mallstable.pojo.SverResponse;
import com.example.mallstable.pojo.User;
import com.example.mallstable.utils.JSONUtils;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.lang.reflect.Type;

import okhttp3.Call;

public class AddressAdd extends AppCompatActivity {
    private EditText UserAddrEdit;
    private EditText UserTelEdit;
    private EditText provinceEditEdit;
    private EditText addaddrEditEdit;
    private EditText cityEditEdit;
    private EditText districrEdit;
    private String UserAddr;
    private String UserTel;
    private String provinceEdit ;
    private String cityEdit ;
    private String addaddrEdit;
    private String district;
    private Toolbar toolbar;
    private Button savebtn;
    private EditText zipEditEdit;
    private String zip;

    /*添加地址*/
    Address address = new Address();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_addr);

        toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("新增地址");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        savebtn = (Button) findViewById(R.id.saveaddr) ;
        savebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addaddr();
                finish();
            }
        });
    }

    private void addaddr() {
        UserAddrEdit = (EditText) findViewById(R.id.UserAddr);
        UserTelEdit = (EditText) findViewById(R.id.UserTel);
        provinceEditEdit = (EditText) findViewById(R.id.provinceEdit);
        cityEditEdit = (EditText) findViewById(R.id.cityEdit);
        addaddrEditEdit = (EditText) findViewById(R.id.addaddrEdit);
        districrEdit = (EditText)findViewById(R.id.districtEdit) ;
        zipEditEdit= (EditText)findViewById(R.id.zipEdit) ;

        UserAddr = UserAddrEdit.getText().toString();
        UserTel = UserTelEdit.getText().toString();
        provinceEdit = provinceEditEdit.getText().toString();
        cityEdit = cityEditEdit.getText().toString();
        district =districrEdit.getText().toString();
        addaddrEdit = addaddrEditEdit.getText().toString();
        zip= zipEditEdit.getText().toString();

        address.setName(UserAddr);
        address.setPhone(UserTel);
        address.setProvince(provinceEdit);
        address.setCity(cityEdit);
        address.setDistrict(district);
        address.setAddr(addaddrEdit);
        address.setZip(zip);

        OkHttpUtils.post()
                .url(Constant.API.USER_ADDR_ADD_URL)
             //   .addParams("address",JSONUtils.toJSON(address))

                .addParams("name",UserAddr)
                .addParams("mobile",UserTel)
                .addParams("province",provinceEdit)
                .addParams("city",cityEdit)
                .addParams("district",district)
                .addParams("addr",addaddrEdit)
                .addParams("zip",zip)

                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("添加地址失败",e.toString());
                    }

                     @Override
                    public void onResponse(String response, int id) {
                         Log.e("添加地址加载", "地址添加成功");
                         //Type type=new TypeToken<SverResponse<Address>>(){}.getType();
                         //SverResponse<Address> result= JSONUtils.formJson(response,type);
                         //if (result.getStatus() == ResponeCode.SUCCESS.getCode()) {
                             //跳转到登陆页面
                             Intent intent = new Intent(AddressAdd.this, AddressListActivity.class);
                             startActivity(intent);
                         //}
                     }
                });

}
}

